<template>
  <div>
    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区域-->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="9">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            @clear="getOrderList"
            clearable
          >
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!--商品列表区域-->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格（元）" width="95px"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="80px">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status==='1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="70px"></el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="170px">
          <template slot-scope="scope">
            {{scope.row.create_time|dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template v-slot="scope">
            <!--修改地址按钮-->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.order_id)"></el-button>
            <!--物流按钮-->
            <el-button type="success" icon="el-icon-location" size="mini" @click="showProgressDialog(scope.row.order_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--页码-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
    <!--修改地址对话框-->
    <el-dialog
      title="修改地址"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form :rules="editAddressFormRules" label-width="90px" :model="editAddressForm" ref="editAddressFormRef">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="editAddressForm.address1"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
            style="width: 100%"
            @change="handleChange">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editAddressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!--物流进度对话框-->
    <el-dialog
      title="修改地址"
      :visible.sync="progressDialogVisible"
      width="50%"
    >
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="progressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import cityData from "./citydata"; "./citydata"
  export default {
    name: "Order",
    data() {
      return {
        //获取商品列表的参数对象
        queryInfo:{
          query:"",
          //当前的页数
          pagenum:1,
          //当前显示数据数量
          pagesize:10
        },
        orderList:[],
        //数据总数
        total:0,
        //修改地址对话框是否显示
        editDialogVisible:false,
        //物流进度对话框是否显示
        progressDialogVisible:false,
        //修改地址对话框绑定数据
        editAddressForm:{
          address1:[],
          address2:""
        },
        //物流进度对话框绑定数据
        progressInfo:{},
        //表单验证规则
        editAddressFormRules:{
          address1:[{ required: true, message: '请选择省市区/县', trigger: 'blur' }],
          address2:[{ required: true, message: '请输入详细地址', trigger: 'blur' }]
        },
        //省市区/县级联数据
        cityData:cityData,
      }
    },
    created() {
      this.getOrderList();
    },
    methods:{
      async getOrderList(){
        const result=(await this.$http.get("orders",{params: this.queryInfo})).data;
        if(result.meta.status!==200) return this.$message.error("请求订单列表失败");
        this.orderList=result.data.goods;
        this.total=result.data.total;
      },
      //监听pagesize改变事件
      handleSizeChange(newSize){
        this.queryInfo.pagesize=newSize;
        this.getOrderList();
      },
      //监听页码值改变事件
      async handleCurrentChange(newSize){
        this.queryInfo.pagenum=newSize;
        this.getOrderList();
      },
      //级联选中项发生变化
      handleChange(){
        if(this.editAddressForm.address1.length!==3){
          this.editAddressForm.address1=[];
        }
      },
      //点击显示修改订单对话框
      showEditDialog(){
        this.editDialogVisible=true;
      },
      //关闭修改订单对话框时触发
      editDialogClosed(){
        this.$refs.editAddressFormRef.resetFields();
      },
      //显示物流进度对话框
      async showProgressDialog(){
        const result=(await this.$http.get("/kuaidi/1106975712662")).data;
        if(result.meta.status!==200) return this.$message.error("请求物流信息出错");
        this.progressInfo=result.data;
        this.progressDialogVisible=true;
      },
    }
  }
</script>

<style scoped lang="less">

</style>
